#include "Function.h"
#include "LED.h"
// #include "RF.h"
#include "UserApp.h"
#include "OS_Event.h"
#include "Config.h"
#include "IRTX.h"
#include "KEY.h"
#include "OS_Timer.h"
#include "EEPROM.h"
#include <string.h>

Typedef_RemoterPara RemoterCtrl = {0};
vuint8 DisplaySel = 0;
Typedef_PARA_SAVE EEP_Para = {0};

const uint8 TAB_KEY_CMD[] = {
    KF_CMD_NULL,
    KF_CMD_YIDONG,
    KF_CMD_DACHONG,
    KF_CMD_XIAOCHONG,
    KF_CMD_TUNXI,
    KF_CMD_FUXI,
    KF_CMD_SHUIWEN,
    KF_CMD_NUANFENG,
    KF_CMD_FENGWEN,
    KF_CMD_ZUOWEN,
    KF_CMD_WEIZHI_JIA,
    KF_CMD_WEIZHI_JIAN,
    KF_CMD_YEDENG,
    KF_CMD_TINGZHI,
    KF_CMD_PAIR,
    KF_CMD_SHUIYA_JIA,
    KF_CMD_SHUIYA_JIAN,
    KF_CMD_JIAOGAN,
    KF_CMD_RUNCI,
    KF_CMD_ZIDONGCHONGSHUI,
    KF_CMD_JIENENG,             // 节能模式
    KF_CMD_JINGXI,              // 劲洗-带水压
    KF_CMD_FANGAI,              // 翻盖
    KF_CMD_FANQUAN,             // 翻圈
    KF_CMD_GUANGAI,             // 关盖
    KF_CMD_ZUOWEN_JIA,          // 座温+   
    KF_CMD_ZUOWEN_JIAN,         // 座温-
    KF_CMD_SHUIWEN_JIA,         // 水温+
    KF_CMD_SHUIWEN_JIAN,        // 水温-
    KF_CMD_HENGJINGCHUJUN,      // 恒净除菌
    KF_CMD_SHOUDONG_GUANGGAN,   // 手动/光感切换夜灯模式
    KF_CMD_DIANJIESHUI,         // 电解水【妇洗+停止】
    KF_CMD_CHUCHOU,             // 除臭【水温减+风温】
    KF_CMD_FENGMINGQI,          // 蜂鸣器【水温+停止】
    KF_CMD_PENTOUQINGXI,        // 喷头清洗【位置上+停止】
    KF_CMD_DUJIAMOSHI,          // 度假模式【移动+停止】
    KF_CMD_GUANGAICHONGSHUI,    // 关盖冲水【关盖+冲水】
    KF_CMD_YANSHIMOSHI,         // 演示模式【臀洗+停止】
    KF_CMD_ZIDONGGUANGAI,       // 自动关盖【长按关盖】
};
#if (_MODULE_SEL_ == _MODULE_N5_)

const uint8 TAB_KEY_MAP_SHORT[] = {
    KF_TINGZHI,         //[1]
    KF_FUXI,            //[2]
    KF_TUNXI,           //[3]
    KF_NUANFENG,        //[4]
    KF_DACHONG,         //[5]
    KF_ZUOWEN_JIA,      //[6]
    KF_ZUOWEN_JIAN,     //[7]
    KF_ZIDONGCHONGSHUI, //[8]
    KF_YEDENG,          //[9]
    KF_HENGJINGCHUJUN,  //[10]
    KF_XIAOCHONG,       //[11]
    KF_SHUIYA_JIAN,     //[12]
    KF_SHUIYA_JIA,      //[13]
    KF_WEIZHI_JIAN,     //[14]
    KF_WEIZHI_JIA,      //[15]
    KF_SHUIWEN_JIAN,    //[16]
    KF_SHUIWEN_JIA,     //[17]
    KF_YIDONG,          //[18]
    KF_YIDONG,          //[19]
    KF_JIENENG,         //[20]
    KF_JIENENG,         //[21]
    KF_NULL,            //[22]
    KF_NULL,            //[23]
    KF_NULL,            //[24]
    KF_NULL,            //[25]
    KF_NULL,            //[26] - 组合1【妇洗+停止】
    KF_NULL,            //[27] - 组合2【水温加+风温】
    KF_NULL,            //[28] - 组合3【水温加+停止】
    KF_NULL,            //[29] - 组合4【位置加+停止】
    KF_NULL,            //[30] - 组合5【水温加+冲水】
    KF_NULL,            //[31] - 组合6【移动1+停止】
    KF_NULL,            //[32] - 组合7【移动2+停止】
    KF_NULL,            //[33] - 组合8【臀洗+停止】
    KF_NULL,            //[34] - 组合9【关盖+冲水】
    KF_YIDONG,          //[35] - 组合10【移动1+移动2】
    KF_JIENENG,         //[36] - 组合11【节能1+节能2】
    KF_NULL,            //[37] - 组合12【移动1+移动2+停止】
};

const uint8 TAB_KEY_MAP_LONG[] = {
    KF_NULL,            //[1]
    KF_NULL,            //[2]
    KF_NULL,            //[3]
    KF_NULL,            //[4]
    KF_NULL,            //[5]
    KF_NULL,            //[6]
    KF_NULL,            //[7]
    KF_NULL,            //[8]
    KF_SHOUDONG_GUANGGAN,//[9]
    KF_NULL,            //[10]
    KF_NULL,            //[11]
    KF_NULL,            //[12]
    KF_NULL,            //[13]
    KF_NULL,            //[14]
    KF_NULL,            //[15]
    KF_NULL,            //[16]
    KF_NULL,            //[17]
    KF_NULL,            //[18]
    KF_NULL,            //[19]
    KF_NULL,            //[20]
    KF_NULL,            //[21]
    KF_NULL,            //[22]
    KF_NULL,            //[23]
    KF_NULL,            //[24]
    KF_NULL,            //[25]
    KF_DIANJIESHUI,     //[26] - 组合1【妇洗+停止】
    KF_CHUCHOU,         //[27] - 组合2【水温加+风温】
    KF_FENGMINGQI,      //[28] - 组合3【水温加+停止】
    KF_PENTOUQINGXI,    //[29] - 组合4【位置加+停止】
    KF_RUNCI,           //[30] - 组合5【水温加+冲水】
    KF_DUJIAMOSHI,      //[31] - 组合6【移动1+停止】
    KF_DUJIAMOSHI,      //[32] - 组合7【移动2+停止】
    KF_YANSHIMOSHI,     //[33] - 组合8【臀洗+停止】
    KF_NULL,            //[34] - 组合9【关盖+冲水】
    KF_NULL,            //[35] - 组合10【移动1+移动2】
    KF_NULL,            //[36] - 组合11【节能1+节能2】
    KF_DUJIAMOSHI,      //[37] - 组合12【移动1+移动2+停止】
};


// #elif (_MODULE_SEL_ == _MODULE_N5S_)
#else

const uint8 TAB_KEY_MAP_SHORT[] = {
    KF_TINGZHI,         //[1]
    KF_FUXI,            //[2]
    KF_TUNXI,           //[3]
    KF_NUANFENG,        //[4]
    KF_DACHONG,         //[5]
    KF_ZUOWEN_JIA,      //[6]
    KF_ZUOWEN_JIAN,     //[7]
    KF_ZIDONGCHONGSHUI, //[8]
    KF_YEDENG,          //[9]
    KF_HENGJINGCHUJUN,  //[10]
    KF_XIAOCHONG,       //[11]
    KF_SHUIYA_JIAN,     //[12]
    KF_SHUIYA_JIA,      //[13]
    KF_WEIZHI_JIAN,     //[14]
    KF_WEIZHI_JIA,      //[15]
    KF_SHUIWEN_JIAN,    //[16]
    KF_SHUIWEN_JIA,     //[17]
    KF_YIDONG,          //[18]
    KF_GUANGAI,         //[19]
    KF_FANGAI,          //[20]
    KF_FANQUAN,         //[21]
    KF_NULL,            //[22]
    KF_NULL,            //[23]
    KF_NULL,            //[24]
    KF_NULL,            //[25]
    KF_NULL,            //[26] - 组合1
    KF_NULL,            //[27] - 组合2
    KF_NULL,            //[28] - 组合3
    KF_NULL,            //[29] - 组合4
    KF_NULL,            //[30] - 组合5
    KF_NULL,            //[31] - 组合6
    KF_NULL,            //[32] - 组合7
    KF_NULL,            //[33] - 组合8
    KF_NULL,            //[34] - 组合9
    KF_NULL,            //[35] - 组合10【移动+关盖】
    KF_NULL,            //[36] - 组合11【翻盖+翻圈】
    KF_NULL,            //[37] - 组合12【移动+关盖+停止】
};


const uint8 TAB_KEY_MAP_LONG[] = {
    KF_JIENENG,         //[1]
    KF_NULL,            //[2]
    KF_NULL,            //[3]
    KF_NULL,            //[4]
    KF_NULL,            //[5]
    KF_NULL,            //[6]
    KF_NULL,            //[7]
    KF_NULL,            //[8]
    KF_SHOUDONG_GUANGGAN,//[9]
    KF_NULL,            //[10]
    KF_NULL,            //[11]
    KF_NULL,            //[12]
    KF_NULL,            //[13]
    KF_NULL,            //[14]
    KF_NULL,            //[15]
    KF_NULL,            //[16]
    KF_NULL,            //[17]
    KF_NULL,            //[18]
    KF_ZIDONGGUANGAI,   //[19]
    KF_NULL,            //[20]
    KF_NULL,            //[21]
    KF_NULL,            //[22]
    KF_NULL,            //[23]
    KF_NULL,            //[24]
    KF_NULL,            //[25]
    KF_DIANJIESHUI,     //[26] - 组合1
    KF_CHUCHOU,         //[27] - 组合2
    KF_FENGMINGQI,      //[28] - 组合3
    KF_PENTOUQINGXI,    //[29] - 组合4
    KF_RUNCI,           //[30] - 组合5
    KF_DUJIAMOSHI,      //[31] - 组合6
    KF_NULL,            //[32] - 组合7
    KF_YANSHIMOSHI,     //[33] - 组合8
    KF_GUANGAICHONGSHUI,//[34] - 组合9
    KF_NULL,            //[35] - 组合10【移动+关盖】
    KF_NULL,            //[36] - 组合11【翻盖+翻圈】
    KF_NULL,            //[37] - 组合12【移动+关盖+停止】
};

#endif



uint8 CalcCheckSum(uint8 *buf, uint8 len, uint8 init_value)
{
    uint8 i,cs = init_value;
    for(i=0; i<len; i++)
    {
        cs += buf[i];
    }
    return cs;
}

void EEP_Save(void)
{
    // uint8 i;
    // EEP_Para.CheckSum = 0;
    memcpy(&EEP_Para.ParaData,&RemoterCtrl,sizeof(Typedef_RemoterPara));

    EEP_Para.CheckSum = CalcCheckSum(&EEP_Para.ParaData,sizeof(Typedef_RemoterPara),INIT_CS);
    // for(i=0; i<sizeof(Typedef_RemoterPara); i++)
    // {
    //     EEP_Para.CheckSum += ((uint8 *)&EEP_Para.ParaData)[i];
    // }
    Memory_Write(EEP_ADDR_PARA,&EEP_Para,sizeof(Typedef_PARA_SAVE));
}

void EEP_Load(void)
{
    Memory_Read(EEP_ADDR_PARA,&EEP_Para,sizeof(Typedef_PARA_SAVE));
    if( CalcCheckSum(&EEP_Para.ParaData,sizeof(Typedef_RemoterPara),INIT_CS) == EEP_Para.CheckSum)
    {
        memcpy(&RemoterCtrl, &EEP_Para.ParaData, sizeof(Typedef_RemoterPara));
    }
    else
        initFunction();
}


const uint8 TAB_LED_LEVEL[] ={
    0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF
};

bit f_Operator_Dir = 0;
// const uint8 TAB_FENGWEN[] = {0x00,0x01,0x03,0x05};

// const uint8 TAB_WEIZHI[] = {0x01,0x03,0x05};

void SwitchDisplay(uint8 disp)
{
    // if(disp != DisplaySel)
    {
        uint8 level;
        DisplaySel = disp;

        switch(DisplaySel)
        {
            case DISP_SHUIWEN:
                level = RemoterCtrl.ShuiWen;
                LED_Set(TAB_LED_LEVEL[level],TOP,(C_DISP_TIME_SET/TIME_BASE_LED),(C_DISP_TIME_SET/TIME_BASE_LED),1,NULL);
            break;

            case DISP_FENGWEN:
                level = RemoterCtrl.FengWen;
                LED_Set(TAB_LED_LEVEL[level],TOP,(C_DISP_TIME_SET/TIME_BASE_LED),(C_DISP_TIME_SET/TIME_BASE_LED),1,NULL);
            break;

            case DISP_ZUOWEN:
                level = RemoterCtrl.ZuoWen;
                LED_Set(TAB_LED_LEVEL[level],BOTTOM,(C_DISP_TIME_SET/TIME_BASE_LED),(C_DISP_TIME_SET/TIME_BASE_LED),1,NULL);
            break;

            case DISP_WEIZHI:
                level = RemoterCtrl.WeiZhi;
                LED_Set(TAB_LED_LEVEL[level],TOP,(C_DISP_TIME_SET/TIME_BASE_LED),(C_DISP_TIME_SET/TIME_BASE_LED),1,NULL);
            break;

            case DISP_SHUIYA:
                level = RemoterCtrl.ShuiYa;
                LED_Set(TAB_LED_LEVEL[level],TOP,(C_DISP_TIME_SET/TIME_BASE_LED),(C_DISP_TIME_SET/TIME_BASE_LED),1,NULL);
            break;

            case DISP_PAIR:
                LED_Set(ALL_ON,TOP|BOTTOM,500/TIME_BASE_LED,1000/TIME_BASE_LED,25,NULL);
            break;          

            case DISP_OFF:
            default:
                LED_Set(ALL_OFF,TOP,0,0,0,NULL);
            break;
        }
    }
}

uint8 GetDisplaySel(void)
{
    return DisplaySel;
}

// void DisplayLevel(uint8 level)
// {
//     LED_Set(TAB_LED_LEVEL[level],3000/TIME_BASE_LED,3100/TIME_BASE_LED,1);
// }

void DisplayClear(void)
{
    LED_Set(ALL_OFF,TOP,0,0,0,NULL);
}


void initFunction(void)
{
    RemoterCtrl.FengWen = C_DEFAULT_FENGWEN;
    RemoterCtrl.ShuiWen = C_DEFAULT_SHUIWEN;
    RemoterCtrl.ZuoWen = C_DEFAULT_ZUOWEN;
    RemoterCtrl.ShuiYa = C_DEFAULT_SHUIYA;
    RemoterCtrl.WeiZhi = C_DEFAULT_WEIZHI;
    RemoterCtrl.YiDong = 0;
    RemoterCtrl.YeDeng = 0;
    RemoterCtrl.JiaoGan = 0;
    RemoterCtrl.RunCi = 0;
    RemoterCtrl.ZiDongChongShui = 0;
    RemoterCtrl.JieNeng = 0;
    RemoterCtrl.ShouDong = 0;
    RemoterCtrl.DianJieShui = 0;
    RemoterCtrl.ChuChou = 0;
    RemoterCtrl.FengMingQi = 0;
    RemoterCtrl.DuJiaMoShi = 0;
    RemoterCtrl.GuanGaiChongShui = 0;
    
}

uint8 CntPairSendTimes = 0;
void FCB_AutoSendPair(void)
{
    if(CntPairSendTimes)    CntPairSendTimes--;
    if(CntPairSendTimes == 0)
    {
        OS_StopTimer(TIMER_PAIR);
    }
    else
    {
        IR_TxBuf[11] = 0x01;
        IR_Send(KF_CMD_PAIR);
        OS_StartTimer(TIMER_PAIR,TIMER_1S,FCB_AutoSendPair);
    }

    
}

void Key_FunctionX(uint8 key)
{
    uint8 update_eep = NO;
    
    if(key == KF_NULL)
        return;


    //---- 停止键上电常亮过程转码对码 ----//
    if((key == KF_TINGZHI) && (PowerOnState))
    {
        key = KF_PAIR;
    }

    PowerOnState = 0;
    OS_StopTimer(TIMER_PAIR);
    
    switch(key)
    {
        //---- 移动 ----//
        case KF_YIDONG:
            IR_TxBuf[11] = RemoterCtrl.YiDong;
            RemoterCtrl.YiDong = ~RemoterCtrl.YiDong;
            SwitchDisplay(DISP_OFF);
            IR_Send(TAB_KEY_CMD[key]);
            update_eep = YES;
        break;

        //---- 停止 ----//
        case KF_TINGZHI:
        //---- 小冲 ----//
        case KF_XIAOCHONG:
        //---- 大冲 ----//
        case KF_DACHONG:
        //---- 翻盖 ----//
        case KF_FANGAI:
        //---- 翻圈 ----//
        case KF_FANQUAN:
        //---- 恒净除菌 ----//
        case KF_HENGJINGCHUJUN:
        //---- 关盖 ----//
        case KF_GUANGAI:
        //---- 喷头清洗 ----//
        case KF_PENTOUQINGXI:
        //---- 演示模式 ----//
        case KF_YANSHIMOSHI:
            IR_TxBuf[11] = 0x01;
            SwitchDisplay(DISP_OFF);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 臀洗 ----//
        //---- 妇洗 ----//
        case KF_TUNXI:
        case KF_FUXI:
        case KF_JINGXI:
            IR_TxBuf[11] =  RemoterCtrl.ShuiYa;
            SwitchDisplay(DISP_OFF);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 暖风 ----//
        //---- 风温 ----//
        case KF_NUANFENG:
        case KF_FENGWEN:
            if(GetDisplaySel() == DISP_FENGWEN)
            {
                if(f_Operator_Dir == 0)
                {
                    if(RemoterCtrl.FengWen < 5)
                    {
                        RemoterCtrl.FengWen++;
                        if(RemoterCtrl.FengWen == 5)
                            f_Operator_Dir = 1;
                    }
                }
                else
                {                   
                    if(RemoterCtrl.FengWen > 0)
                    {
                        RemoterCtrl.FengWen--;
                        if(RemoterCtrl.FengWen == 0)
                            f_Operator_Dir = 0;
                    }
                }
                IR_TxBuf[11] = RemoterCtrl.FengWen;
                IR_Send(TAB_KEY_CMD[KF_FENGWEN]);
                
                update_eep = YES;
            }
            else 
            {
                if(RemoterCtrl.FengWen == 0)        f_Operator_Dir = 0;
                else if(RemoterCtrl.FengWen == 5)   f_Operator_Dir = 1;
                IR_TxBuf[11] = RemoterCtrl.FengWen;
                IR_Send(TAB_KEY_CMD[KF_NUANFENG]);
            }
            SwitchDisplay(DISP_FENGWEN);
        break;

        //---- 座温 ----//
        case KF_ZUOWEN:
            if(GetDisplaySel() == DISP_ZUOWEN)
            {
                if(f_Operator_Dir == 0)
                {
                    if(RemoterCtrl.ZuoWen < 5)
                    {
                        RemoterCtrl.ZuoWen++;
                        if(RemoterCtrl.ZuoWen == 5)
                            f_Operator_Dir = 1;
                    }
                }
                else
                {
                    if(RemoterCtrl.ZuoWen > 0)
                    {
                        RemoterCtrl.ZuoWen--;
                        if(RemoterCtrl.ZuoWen == 0)
                            f_Operator_Dir = 0;
                    }
                }
            }
            else
            {
                if(RemoterCtrl.ZuoWen == 0)        f_Operator_Dir = 0;
                else if(RemoterCtrl.ZuoWen == 5)   f_Operator_Dir = 1;
            }
            IR_TxBuf[11] = RemoterCtrl.ZuoWen;
            SwitchDisplay(DISP_ZUOWEN);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 座温 + ----//
        case KF_ZUOWEN_JIA:
            if(GetDisplaySel() == DISP_ZUOWEN)
            {
                if(RemoterCtrl.ZuoWen < 5)
                { 
                    RemoterCtrl.ZuoWen++;
                    update_eep = YES;
                }
            }
            IR_TxBuf[11] = RemoterCtrl.ZuoWen;
            SwitchDisplay(DISP_ZUOWEN);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 座温 - ----//
        case KF_ZUOWEN_JIAN:
            if(GetDisplaySel() == DISP_ZUOWEN)
            {
                if(RemoterCtrl.ZuoWen > 0)
                {
                    RemoterCtrl.ZuoWen--;
                    update_eep = YES;
                }
            }
            IR_TxBuf[11] = RemoterCtrl.ZuoWen;
            SwitchDisplay(DISP_ZUOWEN);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 水温 ----//
        case KF_SHUIWEN:
            if(GetDisplaySel() == DISP_SHUIWEN)
            {
                if(f_Operator_Dir == 0)
                {
                    if(RemoterCtrl.ShuiWen < 5)
                    {
                        RemoterCtrl.ShuiWen++;
                        if(RemoterCtrl.ShuiWen == 5)
                        {
                            f_Operator_Dir = 1;
                        }
                        update_eep = YES;
                    }
                }
                else
                {
                    if(RemoterCtrl.ShuiWen)
                    {
                        RemoterCtrl.ShuiWen--;
                        if(RemoterCtrl.ShuiWen == 0)
                            f_Operator_Dir = 0;
                        update_eep = YES;
                    }
                }
            }
            else
            {
                if(RemoterCtrl.ShuiWen == 0)        f_Operator_Dir = 0;
                else if(RemoterCtrl.ShuiWen == 5)   f_Operator_Dir = 1;
            }
            IR_TxBuf[11] = RemoterCtrl.ShuiWen;
            SwitchDisplay(DISP_SHUIWEN);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 水温+ ----//
        case KF_SHUIWEN_JIA:
            if(GetDisplaySel() == DISP_SHUIWEN)
            {
                if(RemoterCtrl.ShuiWen < 5)
                {
                    RemoterCtrl.ShuiWen++;
                    update_eep = YES;
                }
            }
            IR_TxBuf[11] = RemoterCtrl.ShuiWen;
            SwitchDisplay(DISP_SHUIWEN);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 水温- ----//
        case KF_SHUIWEN_JIAN:
            if(GetDisplaySel() == DISP_SHUIWEN)
            {
                if(RemoterCtrl.ShuiWen > 0)
                {
                    RemoterCtrl.ShuiWen--;
                    update_eep = YES;
                }
            }
            IR_TxBuf[11] = RemoterCtrl.ShuiWen;
            SwitchDisplay(DISP_SHUIWEN);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 位置+ ----//
        case KF_WEIZHI_JIA:
            if(GetDisplaySel() == DISP_WEIZHI)
            {
                if(RemoterCtrl.WeiZhi < 5)
                {
                    RemoterCtrl.WeiZhi++;
                    update_eep = YES;
                }
            }
            IR_TxBuf[11] = RemoterCtrl.WeiZhi;
            SwitchDisplay(DISP_WEIZHI);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 位置- ----//
        case KF_WEIZHI_JIAN:            
            if(GetDisplaySel() == DISP_WEIZHI)
            {
                if(RemoterCtrl.WeiZhi > 1)
                {
                    RemoterCtrl.WeiZhi--;
                    update_eep = YES;
                }
            }
            IR_TxBuf[11] = RemoterCtrl.WeiZhi;
            SwitchDisplay(DISP_WEIZHI);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 夜灯 ----//
        case KF_YEDENG:
            IR_TxBuf[11] = RemoterCtrl.YeDeng;
            RemoterCtrl.YeDeng = ~RemoterCtrl.YeDeng;
            SwitchDisplay(DISP_OFF);
            IR_Send(TAB_KEY_CMD[key]);
            update_eep = YES;
        break;

        //---- 手动/光感切换 ----//
        case KF_SHOUDONG_GUANGGAN:
            IR_TxBuf[11] = RemoterCtrl.ShouDong;
            RemoterCtrl.ShouDong = ~RemoterCtrl.ShouDong;
            SwitchDisplay(DISP_OFF);
            IR_Send(TAB_KEY_CMD[key]);
            update_eep = YES;
        break;  

        //---- 水压+ ----//
        case KF_SHUIYA_JIA:
            if(GetDisplaySel() == DISP_SHUIYA)
            {
                if(RemoterCtrl.ShuiYa < 5)
                {
                    RemoterCtrl.ShuiYa++;
                    update_eep = YES;
                }
            }
            IR_TxBuf[11] = RemoterCtrl.ShuiYa;
            SwitchDisplay(DISP_SHUIYA);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 水压- ----//
        case KF_SHUIYA_JIAN:          
            if(GetDisplaySel() == DISP_SHUIYA)
            {
                if(RemoterCtrl.ShuiYa > 1)
                {
                    RemoterCtrl.ShuiYa--;
                    update_eep = YES;
                }
            }
            IR_TxBuf[11] = RemoterCtrl.ShuiYa;
            SwitchDisplay(DISP_SHUIYA);
            IR_Send(TAB_KEY_CMD[key]);
        break;

        //---- 脚感 ----//
        case  KF_JIAOGAN:
            IR_TxBuf[11] = RemoterCtrl.JiaoGan;
            RemoterCtrl.JiaoGan = ~RemoterCtrl.JiaoGan;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;

        //---- 润瓷 ----//
        case  KF_RUNCI:
            IR_TxBuf[11] = RemoterCtrl.RunCi;
            RemoterCtrl.RunCi = ~RemoterCtrl.RunCi;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;

        //---- 自动冲水 ----//
        case KF_ZIDONGCHONGSHUI:
            IR_TxBuf[11] = RemoterCtrl.ZiDongChongShui;
            RemoterCtrl.ZiDongChongShui = ~RemoterCtrl.ZiDongChongShui;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;

        //---- 节能 ----//
        case KF_JIENENG:
            IR_TxBuf[11] = RemoterCtrl.JieNeng;
            RemoterCtrl.JieNeng = ~RemoterCtrl.JieNeng;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;              

        //---- 电解水 ----//
        case KF_DIANJIESHUI:
            IR_TxBuf[11] = RemoterCtrl.DianJieShui;
            RemoterCtrl.DianJieShui = ~RemoterCtrl.DianJieShui;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;

        //---- 除臭功能 ----//
        case KF_CHUCHOU:
            IR_TxBuf[11] = RemoterCtrl.ChuChou;
            RemoterCtrl.ChuChou = ~RemoterCtrl.ChuChou;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;

        //---- 蜂鸣器 ----//
        case KF_FENGMINGQI:
            IR_TxBuf[11] = RemoterCtrl.FengMingQi;
            RemoterCtrl.FengMingQi = ~RemoterCtrl.FengMingQi;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;

        //---- 度假模式 ----//
        case KF_DUJIAMOSHI:
            IR_TxBuf[11] = RemoterCtrl.DuJiaMoShi;
            RemoterCtrl.DuJiaMoShi = ~RemoterCtrl.DuJiaMoShi;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;

        //---- 关盖冲水 ----//
        case KF_GUANGAICHONGSHUI:
            IR_TxBuf[11] = RemoterCtrl.GuanGaiChongShui;
            RemoterCtrl.GuanGaiChongShui = ~RemoterCtrl.GuanGaiChongShui;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;

        //---- 自动关盖 ----//
        case KF_ZIDONGGUANGAI:
            IR_TxBuf[11] = RemoterCtrl.ZiDongGuanGai;;
            RemoterCtrl.ZiDongGuanGai = ~RemoterCtrl.ZiDongGuanGai;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_OFF);
            update_eep = YES;
        break;

        //---- 配对 ----//
        case KF_PAIR:
            IR_TxBuf[11] = 0x01;
            IR_Send(TAB_KEY_CMD[key]);
            SwitchDisplay(DISP_PAIR);
            OS_StartTimer(TIMER_PAIR,TIMER_1S,FCB_AutoSendPair);
            CntPairSendTimes = 25;
        break;          


        default:
        break;
    }

    KeyPressTimes++;

    if(update_eep)
    {
        EEP_Save();
    }

}


//=========================================================================
//
//=========================================================================
void DoKey(void)
{
    if(OS_GetEvent(OS_EVENT_SHORT_PRESS))
    {
        OS_ClrEvent(OS_EVENT_SHORT_PRESS);
        Key_FunctionX(TAB_KEY_MAP_SHORT[RKeyCode]);
    }

    if(OS_GetEvent(OS_EVENT_LONG_PRESS))
    {
        OS_ClrEvent(OS_EVENT_LONG_PRESS);
        Key_FunctionX(TAB_KEY_MAP_LONG[RKeyCode]);
    }
}
